#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time

import psutil
import pprint


# 1.CPU各种信息
def show_cpu_info():
    print("CPU的逻辑核心：", psutil.cpu_count(logical=True))
    print("CPU的物理核心：", psutil.cpu_count(logical=False))
    print("CPU的利用率：", psutil.cpu_percent(interval=1))
    print("CPU的使用时间：", psutil.cpu_times())
    print("CPU各种统计：", psutil.cpu_stats())
    print("CPU的频率：", psutil.cpu_freq())


# Memory 内存信息
def show_memory_info():
    mem = psutil.virtual_memory()
    total = mem.total / 1024 / 1024 / 1024
    available = mem.available / 1024 / 1024 / 1024
    used = mem.used / 1024 / 1024 / 1024
    per = mem.percent
    print(f"总内存：{total:.2f}GB")
    print(f"可用内存：{available:.2f}GB")
    print(f"使用内存：{used:.2f}GB")
    print(f"内存使用率：{per}%")
    print("交换分区：", psutil.swap_memory())


# 磁盘信息
def show_disk_info():
    print("磁盘信息：", psutil.disk_partitions(all=False))
    print(psutil.disk_usage('/'))
    # for d in ['C:\\', 'D:\\']:
    #     item = psutil.disk_usage(d)
    #     print(
    #         f'磁盘 {d[0][0]} 总大小 {item[0] / 1024 / 1024 / 1024:.2f}GB 使用 {item[1] / 1024 / 1024 / 1024:.2f}GB 空闲 {item[2] / 1024 / 1024 / 1024:.2f}GB 使用率 {item[3]}%')


# 网络信息
def show_network_info():
    print(psutil.net_io_counters())
    print(psutil.net_connections())
    pprint.pprint(psutil.net_if_addrs())
    print(psutil.net_if_stats())


if __name__ == '__main__':
    print("开机时间：", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(psutil.boot_time())))
    print("当前用户：", psutil.users())
    print(psutil.sensors_battery())
    show_cpu_info()
    show_memory_info()
    show_disk_info()
    show_network_info()
    # pass
